﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Common
{
    public class DateHelper
    {

        private static string[] Days = new string[] { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };

        public static int GetQuarterByDate(DateTime dt)
        {
            int m = dt.Month;
            if (m >= 1 && m <= 3) return 1;
            if (m >= 4 && m <= 6) return 2;
            if (m >= 7 && m <= 9) return 3;
            if (m >= 10 && m <= 12) return 4;
            return 0;
        }

        public static DateTime DefaultDate
        {
            get
            {
               return DateTime.Parse("2015-01-01 00:00:00");
            }
        }

        public static int GetDaysByMonth(DateTime dtNow)
        {
            int days = DateTime.DaysInMonth(dtNow.Year, dtNow.Month);
            return days;
        }
        /// <summary>
        /// 星期几
        /// </summary>
        public static string WeekDay
        {
            get
            {
                return Days[Convert.ToInt16(DateTime.Now.DayOfWeek)];
            }
        }
        /// <summary>
        /// 今天
        /// </summary>
        public static DateTime Today
        {
            get
            {
                return DateTime.Now.Date;
            }
        }
        /// <summary>
        /// 昨天
        /// </summary>
        public static DateTime Yesterday
        {
            get
            {
                return DateTime.Now.AddDays(-1).Date;
            }
        }
        /// <summary>
        /// 明天
        /// </summary>
        public static DateTime Tomorrow
        {
            get
            {
                return DateTime.Now.AddDays(1).Date;
            }
        }
        /// <summary>
        /// 当天
        /// </summary>
        public static DateInfo CurrentDay
        {
            get
            {
                return new DateInfo
                {
                    StartDate = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd")+" 00:00:00"),
                    EndDate = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd")+" 23:59:59")
                };
            }
        }
        /// <summary>
        /// 本周
        /// </summary>
        public static DateInfo CurrentWeek
        {
            get
            {
                return new DateInfo
                {
                    StartDate = DateTime.Now.AddDays(Convert.ToDouble((0 - Convert.ToInt16(DateTime.Now.DayOfWeek)))).Date,
                    EndDate = DateTime.Now.AddDays(Convert.ToDouble((6 - Convert.ToInt16(DateTime.Now.DayOfWeek)))).Date.AddDays(1).AddSeconds(-1)
                };
            }
        }
        /// <summary>
        /// 上周
        /// </summary>
        public static DateInfo LastWeek
        {
            get
            {
                return new DateInfo
                {
                    StartDate = DateTime.Now.AddDays(Convert.ToDouble((0 - Convert.ToInt16(DateTime.Now.DayOfWeek))) - 7).Date,
                    EndDate = DateTime.Now.AddDays(Convert.ToDouble((6 - Convert.ToInt16(DateTime.Now.DayOfWeek))) - 7).Date.AddDays(1).AddSeconds(-1)
                };
            }
        }

        /// <summary>
        /// 下周
        /// </summary>
        public static DateInfo NextWeek
        {
            get
            {
                return new DateInfo
                {
                    StartDate = DateTime.Now.AddDays(Convert.ToDouble((1 - Convert.ToInt16(DateTime.Now.DayOfWeek))) + 7).Date,
                    EndDate = DateTime.Now.AddDays(Convert.ToDouble((7 - Convert.ToInt16(DateTime.Now.DayOfWeek))) + 7).Date.AddDays(1).AddSeconds(-1)
                };
            }
        }
        public static DateInfo MonthStartEnd(int year,int month)
        {
            DateInfo dateinfo = new DateInfo();
            dateinfo.StartDate = DataConvert.ToDateTime(year + "-" + month + "-01 00:00:00");
            dateinfo.EndDate = DataConvert.ToDateTime(year + "-" + month + "-" + DateTime.DaysInMonth(year, month) + " 23:59:59");
            return dateinfo;
        }
        /// <summary>
        /// 本月
        /// </summary>
        public static DateInfo CurrentMouth
        {
            get
            {
                return new DateInfo
                {
                    StartDate = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-01")),
                    EndDate = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-01")).AddMonths(1).AddSeconds(-1)
                };
            }
        }
        /// <summary>
        /// 上月
        /// </summary>
        public static DateInfo LastMouth
        {
            get
            {
                return new DateInfo
                {
                    StartDate = CurrentMouth.StartDate.AddMonths(-1),
                    EndDate = CurrentMouth.StartDate.AddSeconds(-1)
                };
            }
        }
        /// <summary>
        /// 下月
        /// </summary>
        public static DateInfo NextMouth
        {
            get
            {
                return new DateInfo
                {
                    StartDate = CurrentMouth.StartDate.AddMonths(1),
                    EndDate = CurrentMouth.StartDate.AddMonths(2).AddSeconds(-1)
                };
            }
        }
        /// <summary>
        /// 某年度
        /// </summary>
        public static DateInfo YearStartEnd(int year)
        {
            DateInfo dateinfo = new DateInfo();
            dateinfo.StartDate = DataConvert.ToDateTime(year + "-01-01 00:00:00");
            dateinfo.EndDate = DataConvert.ToDateTime(year + "-01-01").AddYears(1).AddSeconds(-1);
            return dateinfo;
        }
        /// <summary>
        /// 本年度
        /// </summary>
        public static DateInfo CurrentYear
        {
            get
            {
                return new DateInfo
                {
                    StartDate = DateTime.Parse(DateTime.Now.ToString("yyyy-01-01")),
                    EndDate = DateTime.Parse(DateTime.Now.ToString("yyyy-01-01")).AddYears(1).AddSeconds(-1)
                };
            }
        }
        /// <summary>
        /// 上年度
        /// </summary>
        public static DateInfo LastYear
        {
            get
            {
                return new DateInfo
                {
                    StartDate = CurrentYear.StartDate.AddYears(-1),
                    EndDate = CurrentYear.StartDate.AddSeconds(-1)
                };
            }
        }
        /// <summary>
        /// 下年度
        /// </summary>
        public static DateInfo NextYear
        {
            get
            {
                return new DateInfo
                {
                    StartDate = CurrentYear.StartDate.AddYears(1),
                    EndDate = CurrentYear.StartDate.AddYears(2).AddSeconds(-1)
                };
            }
        }

        /// <summary>
        /// 本季度
        /// </summary>
        public static DateInfo CurrentQuarter
        {
            get
            {
                return new DateInfo
                {
                    StartDate = DateTime.Parse(DateTime.Now.AddMonths(0 - ((DateTime.Now.Month - 1) % 3)).ToString("yyyy-MM-01")),
                    EndDate = DateTime.Parse(DateTime.Now.AddMonths(3 - ((DateTime.Now.Month - 1) % 3)).ToString("yyyy-MM-01")).AddSeconds(-1)
                };
            }
        }
        /// <summary>
        /// 上季度
        /// </summary>
        public static DateInfo LastQuarter
        {
            get
            {
                return new DateInfo
                {
                    StartDate = DateTime.Parse(DateTime.Now.AddMonths(-3 - ((DateTime.Now.Month - 1) % 3)).ToString("yyyy-MM-01")),
                    EndDate = DateTime.Parse(DateTime.Now.AddMonths(0 - ((DateTime.Now.Month - 1) % 3)).ToString("yyyy-MM-01")).AddSeconds(-1)
                };
            }
        }
        /// <summary>
        /// 下季度
        /// </summary>
        public static DateInfo NextQuarter
        {
            get
            {
                return new DateInfo
                {
                    StartDate = DateTime.Parse(DateTime.Now.AddMonths(3 - ((DateTime.Now.Month - 1) % 3)).ToString("yyyy-MM-01")),
                    EndDate = DateTime.Parse(DateTime.Now.AddMonths(6 - ((DateTime.Now.Month - 1) % 3)).ToString("yyyy-MM-01")).AddSeconds(-1)
                };
            }
        }
        
        public static int[] GetMouths(int sort)
        {
            int[] mouths = new int[0];
            if (sort == 1)
            {
                mouths = new int[3] { 1, 2, 3 };
            }
            else if (sort == 2)
            {
                mouths = new int[3] { 4, 5, 6 };
            }
            else if (sort == 3)
            {
                mouths = new int[3] { 7, 8, 9 };
            }
            else if (sort == 4)
            {
                mouths = new int[3] { 10, 11, 12 };
            }
            else if (sort == 5)
            {
                mouths = new int[6] { 1, 2, 3, 4, 5, 6 };
            }
            else if (sort == 6)
            {
                mouths = new int[6] { 7, 8, 9, 10, 11, 12 };
            }
            else if (sort == 7)
            {
                mouths = new int[12] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
            }
            return mouths;
        }
        

        public static string GetDayText(DateTime dt)
        {
            TimeSpan ts = DateTime.Now - dt;
            if (ts.TotalDays > 0)
            {
                if (ts.TotalDays <= 1)
                {
                    return "今天";
                }
                else if (ts.TotalDays <= 2)
                {
                    return "昨天";
                }
                else if (ts.TotalDays <= 3)
                {
                    return "前天";
                }
            }
            else
            {
                if (-ts.TotalDays <= 1)
                {
                    return "明天";
                }
                else if (-ts.TotalDays <= 2)
                {
                    return "后天";
                }
            }
            //if (dt.Year==DateTime.Now.Year)
            //{
            //    return dt.ToString("MM-dd");
            //}
            return dt.ToString("yyyy-MM-dd");
            //if (DateHelper.LastWeek.StartDate <= dt && DateHelper.LastWeek.EndDate >= dt)
            //{
            //    return "上周";
            //}
            //if (DateHelper.CurrentWeek.StartDate <= dt && DateHelper.CurrentWeek.EndDate>=dt)
            //{
            //    return "本周";
            //}
            //if (DateHelper.NextWeek.StartDate <= dt && DateHelper.NextWeek.EndDate >= dt)
            //{
            //    return "下周";
            //}
            //if (DateHelper.LastMouth.StartDate <= dt && DateHelper.LastMouth.EndDate >= dt)
            //{
            //    return "上月";
            //}
            //if (DateHelper.CurrentMouth.StartDate <= dt && DateHelper.CurrentMouth.EndDate >= dt)
            //{
            //    return "本月";
            //}
            //if (DateHelper.NextMouth.StartDate <= dt && DateHelper.NextMouth.EndDate >= dt)
            //{
            //    return "下月";
            //}

        }
        public class DateInfo
        {
            public DateTime StartDate { get; set; }
            public DateTime EndDate { get; set; }
        }

    }
}
